package disk_rpc

import (
	"context"
	"disk/disk_rpc_server/disk_pb"
	"disk/utils/mysql"
	"github.com/gin-gonic/gin"
)

func Add(r *gin.RouterGroup) {
	r.POST("/add", func(c *gin.Context) {
		var dataList mysql.DiskList
		err := c.ShouldBind(&dataList)
		if err != nil {
			c.JSON(200, gin.H{
				"msg":  "添加失败，数据校验未通过",
				"data": err.Error(),
				"code": 400,
			})
			return
		}

		pb := &disk_pb.DiskList{
			Id:       int64(dataList.ID),
			Name:     dataList.Name,
			Level:    dataList.Level,
			Email:    dataList.Email,
			Phone:    dataList.Phone,
			Birthday: dataList.Birthday,
			Address:  dataList.Address,
		}

		client := NewClient()
		result, err := client.Add(context.Background(), &disk_pb.AddReq{
			List: pb,
		})
		if err != nil {
			c.JSON(200, gin.H{
				"msg":  "添加失败",
				"data": result.Message,
				"code": 400,
			})
		} else {
			c.JSON(200, gin.H{
				"msg":  "添加成功",
				"data": "",
				"code": 200,
			})
		}
	})
}
